package deployr.cli;

import static deployr.core.Streams.*;
import deployr.core.*;

public class CreateContextResponder extends Responder {

	@Override
	protected Result respondTo(String method, String requestURI, Input input) {
		String appId = requestURI.replaceAll(".*/contexts/", "");
		
		FileSystem fsOrigin = new FileSystem(file("/tmp/wars"));
		FileSystem fsTarget = new FileSystem(file("/tmp/jetty-6.1.23/webapps"));
		FileSystem fsContexts = new FileSystem(file("/tmp/jetty-6.1.23/contexts"));
		
		Wars wars = new Wars(fsOrigin, fsTarget);
		JettyContexts contexts = new JettyContexts(fsContexts);
		JettyDriver jettyDriver = new JettyDriver(wars, contexts);

		jettyDriver.deploy(appId);
		
		return new Result(200, "ok");
	}

	@Override
	protected boolean shouldRespond(String method, String requestURI) {
		return method.equals("POST") && requestURI.matches(".*/contexts.*");
	}
}
